package com.uchiha.nova.mainService.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uchiha.nova.core.model.query.BtnInfoQuery;
import com.uchiha.nova.mainService.service.IBtnInfoService;
import com.uchiha.nova.mainService.wrapper.BtnInfoWrapper;
import com.uchiha.nova.core.tool.constant.NovaCRUDMethodConstant;
import com.uchiha.nova.core.model.entity.BtnInfoEntity;

import com.uchiha.nova.core.model.vo.BtnInfoVO;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.support.BaseController;
import com.uchiha.nova.core.tool.support.Condition;
import com.uchiha.nova.core.tool.support.QueryContext;
import com.uchiha.nova.core.tool.util.Func;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


/**
 * 按钮信息 控制器
 *
 * @author magic
 */
@RestController
@AllArgsConstructor
@RequestMapping("/btnInfo")
@Tag(name = "按钮信息")
@Slf4j
public class BtnInfoController extends BaseController {

   @Resource
   IBtnInfoService btnInfoService;

	/**
	 * 详情
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_DETAIL)
	@Operation(summary = "详情")
	public R<BtnInfoVO> detail(@RequestBody BtnInfoEntity btnInfoEntity) {
		BtnInfoEntity detail = btnInfoService.getOne(Condition.getQueryWrapper(btnInfoEntity));
		return R.data(BtnInfoWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 代码自定义代号
	 */
	@GetMapping(NovaCRUDMethodConstant.NOVA_LIST)
    @Operation(summary = "分页查询")
	public R getPage(BtnInfoQuery query, QueryContext queryContext) {
		IPage<BtnInfoEntity> pages = btnInfoService.page(Condition.getPage(queryContext), Condition.getQueryWrapper(query, queryContext,BtnInfoEntity.class));
		return R.data(BtnInfoWrapper.build().pageVO(pages));
	}

	/**
	 * 新增 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_SAVE)
	@Operation(summary = "新增")
	public R save(@RequestBody BtnInfoEntity btnInfoEntity) {
		Long a  = btnInfoService.count(new QueryWrapper<BtnInfoEntity>().eq("code",btnInfoEntity.getCode()));
		if(a.compareTo(0L)==1){
			return R.fail("重复的code");
		}
		return R.status(btnInfoService.save(btnInfoEntity));
	}

	/**
	 * 修改 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_UPDATE)
	@Operation(summary = "修改")
	public R update(@RequestBody BtnInfoEntity btnInfoEntity) {
		BtnInfoEntity a  = btnInfoService.getOne(new QueryWrapper<BtnInfoEntity>().eq("code",btnInfoEntity.getCode()));
		if(ObjectUtil.isNotEmpty(a)){
			if(a.getId().compareTo(btnInfoEntity.getId())!=0){
				return R.fail("重复的code");
			}
			return R.status(btnInfoService.updateById(btnInfoEntity));
		}
		return R.status(btnInfoService.updateById(btnInfoEntity));
	}

	/**
	 * 新增或修改 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_SAVEORUPDATE)
	@Operation(summary = "新增或修改")
	public R saveOrUpdate(@RequestBody BtnInfoEntity btnInfoEntity) {
		return R.status(btnInfoService.saveOrUpdate(btnInfoEntity));
	}


	/**
	 * 删除 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_REMOVE)
	@Operation(summary = "逻辑删除")
	public R remove(@RequestParam String ids) {
		return R.status(btnInfoService.deleteLogic(Func.toLongList(ids)));
	}
  
    /**
	 * 物理删除，不可恢复
	 */
   @Operation(summary = "物理删除")
   @DeleteMapping(NovaCRUDMethodConstant.NOVA_DELETE)
   public R delete(@RequestParam String ids){
     return R.status(btnInfoService.deletePhysics(Func.toLongList(ids)));
   }
}
